class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
public class Text {
    public TreeNode searchBST(TreeNode root, int val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        if(root.val > val) {
            TreeNode tmp = searchBST(root.left,val);
            if(tmp != null) {
                return tmp;
            }
        }else {
            TreeNode tmp = searchBST(root.right,val);
            if(tmp != null) {
                return tmp;
            }
        }
        return null;
    }
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if(root == null) {
            return new TreeNode(val);
        }
        TreeNode cur = root;
        TreeNode prev = null;
        while(cur != null) {
            prev = cur;
            if(cur.val > val) {
                cur = cur.left;
            }else{
                cur = cur.right;
            }
        }
        TreeNode tmp = new TreeNode(val);
        if(prev.val < tmp.val) {
            prev.right = tmp;
        }else {
            prev.left = tmp;
        }
        return root;
    }
}
